#script1903.py - The Raspberry Pie Game
#Written by Blum and Bresnahan
#######################################
#
##### Import Modules & Variables ######
import pygame	#Import PyGame library
import random	#Import Random module
import sys	#Import System module
#
from pygame.locals import *  #Local PyGame constants
#
pygame.init()                #Initialize PyGame objects
#
##### Set up Functions ###############
#
# Delete a Raspberry
def deleteRaspberry (RaspberryDict, RNumber):
	key1 = 'RasLoc' + str(RNumber)
	key2 = 'RasOff' + str(RNumber)
	#
	#Make a copy of Current Dictionary
	NewRaspberry = dict(RaspberryDict)
	#
	del NewRaspberry[key1]
	del NewRaspberry[key2]
	#
	return NewRaspberry
#
# Set up the Game Screen ##############
#
ScreenSize = ScreenWidth,ScreenHeight = 1000,700 
GameScreen=pygame.display.set_mode(ScreenSize)  
#
# Set up the Game Color ###############
blue=0,0,255
#
# Set up the Game Image Graphics ######
#
GameImage="/usr/share/raspberrypi-artwork/raspberry-pi-logo.png"
GameImageGraphic=pygame.image.load(GameImage).convert_alpha()
GameImageGraphic=pygame.transform.scale(GameImageGraphic,(75,75))
#
GameImageLocation=GameImageGraphic.get_rect()  #Current location
#
ImageOffset=[5,5]  #Starting Speed
# 
# Build the Raspberry Dictionary ######
#
RAmount = 17    #Number of Raspberries on screen
Raspberry = {}  #Initialize the dictionary
#
for RNumber in range(RAmount): #Create the Raspberry dictionary
	Position_x = (ImageOffset[0] + RNumber) * random.randint(9,29)
	Position_y = (ImageOffset[1] + RNumber) * random.randint(8,18)
	RasKey = 'RasLoc' + str(RNumber)
	Location = GameImageLocation.move(Position_x, Position_y)
	Raspberry[RasKey] = Location
	RasKey = 'RasOff' + str(RNumber)
	Raspberry[RasKey] = ImageOffset
#
# Setup Game Sound ###################
#
ClickSound=pygame.mixer.Sound('/home/pi/python_games/match1.wav')
#

###### Play the Game ######################################
#
while True:
	for event in pygame.event.get():
		if event.type == pygame.MOUSEBUTTONDOWN:
			for RNumber in range(RAmount): 
				RasLoc = 'RasLoc' + str(RNumber)  
				RasImageLocation = Raspberry[RasLoc]
				if RasImageLocation.collidepoint(pygame.mouse.get_pos()):
					deleteRaspberry(Raspberry,RNumber)
					RAmount = RAmount - 1
					ClickSound.play()
					pygame.time.delay(50)
					if RAmount == 0:
						sys.exit()
	#Redraw the Screen Background ###############
	GameScreen.fill(blue)
	#
	#Move the Raspberries around the screen #####
	for RNumber in range(RAmount): 
		RasLoc = 'RasLoc' + str(RNumber)  
		RasImageLocation = Raspberry[RasLoc]
		RasOff = 'RasOff' + str(RNumber)
		RasImageOffset = Raspberry[RasOff]
		#
		NewLocation = RasImageLocation.move(RasImageOffset)
		#
		Raspberry[RasLoc] = NewLocation   #Update location
		#
	#Keep Raspberries on screen ##################
		if NewLocation.left < 0 or NewLocation.right > ScreenWidth:
			NewOffset = -RasImageOffset[0]
			if NewOffset < 0:
				NewOffset = NewOffset - 1
			else:
				NewOffset = NewOffset + 1
			#
			RasImageOffset = [NewOffset, RasImageOffset[1]]
			Raspberry[RasOff] = RasImageOffset #Update offset
			#
		if NewLocation.top < 0 or NewLocation.bottom > ScreenHeight:
			NewOffset = -RasImageOffset[1]
			if NewOffset < 0:
				NewOffset = NewOffset - 1
			else:
				NewOffset = NewOffset + 1
			#
			RasImageOffset = [RasImageOffset[0],NewOffset]
			Raspberry[RasOff] = RasImageOffset #Update offset
		#
		GameScreen.blit(GameImageGraphic,NewLocation) #Put on Screen
	#
	pygame.display.update()
#

